import os
import cgi

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import auth
import datastore


# Request handlers

class WebtreePage(webapp.RequestHandler):
    def get(self):
        template_values = {}

        name_ = cgi.escape(self.request.get('name'))
        template_values['name'] = name_
        key_ = cgi.escape(self.request.get('key'))
        template_values['key'] = key_

        if name_ == '' and key_ == '':
            self.response.out.write('<h3>Error: Please specify a webtree "name" or "key".</h3><p>Use a parameter like ?name=example in your url.</p>')
            return False



        # the whole auth business  ( Note we DONT REQUIRE user LOGGED IN! )
        request_url = '/'
        auth_text = 'login'
        auth_url = users.create_login_url(request_url)
        user = users.get_current_user()

        if user:
            nickname = user.nickname()
            email = user.email()

            auth_text = 'logout ' + nickname + ' <' + email + '>'
            auth_url = users.create_logout_url(request_url)

        template_values['auth_url'] = auth_url
        template_values['auth_text'] = auth_text

        # help the template a bit with showing the right html
        template_values['html_mainmenu'] = '<span>[<a href="/">back to list</a>]</span>'
        if user:
            template_values['html_mainmenu'] = '<span>[<a href="/">main menu</a>]</span>'





        #
        # TODO: we need to get the content
        template_values['contents'] = ''

        # 1) if we only got a name as parameter, handle that case such that
        #    it returns a unique key instead

        nop = ''
    
        # 2) now we have our KEY, check if the current (anonymous) user has:
        #    - view permission
        #    - edit permission
    
        nop = ''

        # 3) if we're still here, we have at least view, and finally get 'contents'

        nop = ''
        
        # 4) done!


        # just do 'name' only and assume its unique
        t = datastore.Webtree.all()
        t.filter("name=",template_values['name'])
        t.fetch(1)
        for i in t:
            template_values['contents'] = i.contents

        # generate the django template
        #
        # template_values = 
        # - name: request.get('name')
        # - key: request.get('key') OR determined from request.get('name')
        # - auth_text and auth_url:
        # - html_mainmenu: showing the right html depending on being logged in
        # - content: the JSON content of the webtree record
        auth.request_generate(self,'templates/webtree.html',template_values)


# Url switching table

application = webapp.WSGIApplication([
        ('/webtree', WebtreePage),
        ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
